package com.lmy.mallback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmy.mallback.entity.BigType;
import com.lmy.mallback.entity.Product;
import com.lmy.mallback.entity.SmallType;
import com.lmy.mallback.service.BigTypeService;
import com.lmy.mallback.service.ProductService;
import com.lmy.mallback.service.SmallTypeService;
import com.lmy.mallback.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 程序员小洋
 * @version 2022.3
 * @date 2023/1/11 14:03
 * @title 商品
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/bigType")
public class BigTypeController {

    @Autowired
    private BigTypeService bigTypeService;

    @Autowired
    private SmallTypeService smallTypeService;

    @Autowired
    private ProductService productService;

    /**
     * 查询所有商品大类
     * @return
     */
    @GetMapping("/findAll")
    public R findAll(){
        List<BigType> bigTypeList = bigTypeService.list();
        Map<String,Object> map=new HashMap<>();
        map.put("message",bigTypeList);
        return R.ok(map);
    }

    @GetMapping("/findCategories")
    public R findCategories(){

        // 获取商品大类
        List<BigType> bigTypeList = bigTypeService.list();

        /**
         * 获取商品小类
         * 思路:
         * 1.遍历商品大类
         * 2.通过商品小类表中的外键bigTypeId字段和商品大类表中的id字段相等来判断属于哪个大类
         * 3.通过商品表中的外键TypeId字段和商品小类中的id字段相等来判断属于哪个小类
         * 4.把数据返回给前端
         */
        for (BigType bigType : bigTypeList) {
            List<SmallType> smallTypeList = smallTypeService.list(new QueryWrapper<SmallType>().eq("bigTypeId", bigType.getId()));
            bigType.setSmallTypeList(smallTypeList);
            for (SmallType smallType : smallTypeList) {
                List<Product> productList = productService.list(new QueryWrapper<Product>().eq("typeId", smallType.getId()));
                smallType.setProductList(productList);
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("message",bigTypeList);
        return R.ok(map);
    }
}
